package com.example.codegen;

import com.example.utils.CJUtils;

public class NewArrayExpression implements Expression {
    public Type type;
    public long dimensions;
    // may use variable here
    public Variables length;

    public NewArrayExpression(Type type, int dimensions, Variables length) {
        this.type = type;
        this.dimensions = dimensions;
        this.length = length;
    }

    @Override
    public String toJava() {
        assert dimensions > 0;
        return "new " + type.toJava() + "[" + length.toJava() + "]" + "[]".repeat((int) (dimensions - 1));
    }

    @Override
    public String toGo() {
        return "make(" + "[]".repeat((int) dimensions) + type.toGo() + ", " + length.toGo() + ")";
    }

    @Override
    public String toJs() {
        return "new Array(" + length.toJs() + ")";
    }

    @Override
    public String toCSharp() {
        return "new " + type.toCSharp() + "[" + length.toCSharp() + "]" + "[]".repeat((int) (dimensions - 1));
    }

    public String toCJ(){
        assert dimensions > 0;
        String innerType = type.toCJ();
        if(innerType.startsWith("Option<"))
            innerType = innerType.substring(7, innerType.length()-1);
        for (int i = 0; i < dimensions - 1; i++) {
            innerType = "?Array<" + innerType + ">";
        }

        String initVal = CJUtils.getDefaultValue(innerType);

        return "Array<" + innerType + ">("+ length.toCJ() + ", item: "+ initVal + ")";
    }
}
